package com.irs.controller;


import javax.smartcardio.ATR;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.irs.annotation.SysLog;
import com.irs.pojo.ArticleSearch;
import com.irs.pojo.TbArticle;
import com.irs.service.ArticleService;
import com.irs.service.CategoryService;
import com.irs.util.ResultUtil;


@Controller
@RequestMapping("article/")
public class ArticleController {
	@Autowired
	private ArticleService articleServiceImpl;
	@Autowired
	private CategoryService categoryService;
	
	@RequestMapping("articleList")
	@RequiresPermissions("article:article:list")
	public String selArticle(Model model){
		model.addAttribute("categoryList",categoryService.selectAll());
		return "page/article/articleList";
	}
	
	@RequestMapping("addArticle")
	@RequiresPermissions("article:article:save")
	public String addArticle(Model model){
		model.addAttribute("categoryList",categoryService.selectAll());
		return "page/article/addArticle";
		
	}
	@SysLog(value="修改文章")
	@RequestMapping("editArticle/{id}")
	@RequiresPermissions("article:article:update")
	public String editArticle(@PathVariable("id")String id,Model model){
		
		TbArticle tbArticle = articleServiceImpl.selArticleById(id);
		model.addAttribute("categoryList",categoryService.selectAll());
		model.addAttribute("article",tbArticle);
		return "page/article/editArticle";
		
	}
	
	@SysLog(value="文章列表")
	@RequestMapping("selArticleList")
	@RequiresPermissions("article:article:list")
	@ResponseBody
	public ResultUtil selArticleList(Integer page,Integer limit,ArticleSearch search){
		return articleServiceImpl.selArticle(page, limit, search);
		
	}
	@SysLog(value="添加文章")
	@RequestMapping("insArticle")
	@RequiresPermissions("article:article:save")
	@ResponseBody
	public ResultUtil insArticle(TbArticle tbArticle){
		int a =  articleServiceImpl.insService(tbArticle);
		if(a>0){
			return ResultUtil.ok();
		}else{
			return  new ResultUtil(500,"文章新增失败");
		}
		
	}
	
	@SysLog(value="修改文章")
	@RequestMapping("updateArticle")
	@RequiresPermissions("article:article:update")
	@ResponseBody
	public ResultUtil updateById(TbArticle tbArticle){
		int result = articleServiceImpl.updateById(tbArticle);
		if(result >0){
			return ResultUtil.ok();
		}else{
			return  new ResultUtil(500,"文章修改失败");
		}
		
	}
	
	@SysLog(value="根据id删除")
	@RequestMapping("deleteById")
	@RequiresPermissions("article:article:delete")
	@ResponseBody
	public ResultUtil deleteById(String id){
		
		try {
			articleServiceImpl.deleteById(id);
			return ResultUtil.ok(); 
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResultUtil.error();
		}	
	}
	
	@SysLog(value="批量删除")
	@RequestMapping("deleteAll")
	@RequiresPermissions("article:article:delete")
	@ResponseBody
	public ResultUtil deleteAll(String IdStr){
		boolean result= articleServiceImpl.deleteAll(IdStr);
		if(result){
			return ResultUtil.ok();
		}else{
			return ResultUtil.error();
		}

	}
	
	
}
